package com.example.falvback.service;

import com.example.falvback.entity.User;
import com.example.falvback.exception.ServiceException;
import com.example.falvback.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：用户信息的service层
 * 作者：chen
 * 日期：2023/10/2220:20
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;//自动注入mapper层

    public List<User> showUser() {
        return userMapper.showUser();
    }

    public List<User> showUserFile(String username) {
        return userMapper.showUserFile(username);
    }

    public void updateUser(User user) {
        userMapper.updateUser(user);
    }
    //  Service代码：
    public void resetPassword(User user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser == null) {
            // 抛出一个自定义的异常
            throw new ServiceException("用户不存在");
        }
        if (!user.getPhone().equals(dbUser.getPhone())) {
            throw new ServiceException("验证错误");
        }
        dbUser.setPassword("123");   // 重置密码
        userMapper.updateUser(dbUser);
    }
}
